/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package goodenglish.domain.model.subt;

import goodenglish.domain.model.Lazy;
import goodenglish.domain.model.LazyList;
import goodenglish.domain.model.lexicon.Lexicon;
import goodenglish.domain.model.lexicon.LexiconRepository;
import goodenglish.infrastructure.repository.db.LexiconRepositoryDb;
import goodenglish.infrastructure.repository.db.SubtRepositoryDb;
import goodenglish.interfaces.BeanFactory;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author 关攀攀 panpan_001@126.com
 */
public class Subt {
	private static Log logger = LogFactory.getLog(Subt.class);
	private String id;
	private String name;

	private Lexicon lexicon;

	private List<SubtSentence> subtSetences ;

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public List<SubtSentence> getSubtSentences() {
		if (subtSetences instanceof LazyList) {
			subtRepository.loadSubtSentence(this);
		}
		return subtSetences;
	}

	public void setSubtSetences(List<SubtSentence> subtSetences) {
		this.subtSetences = subtSetences;
	}

	public void setLexicon(Lexicon lexicon) {

		this.lexicon = lexicon;
	}

	public Lexicon getLexicon() {
		if (lexicon instanceof Lazy) {
			// 加载生词

			this.lexicon = lexiconRepository.getLexicon(this.lexicon.getId());
		}
		return lexicon;
	}

	/**
	 * 得到含有单词的句子
	 * 
	 * @param subtWord
	 * @return
	 */
	public List<SubtSentence> getSubtSentences(String spell) {
		List<SubtSentence> sentences = new ArrayList<SubtSentence>();
		for (int i = 0; i < this.getSubtSentences().size(); i++) {
			if (this.getSubtSentences().get(i).containsWord(spell)) {

				sentences.add(this.getSubtSentences().get(i));
			}
		}
		return sentences;
	}

	/**
	 * 得到含有单词的句子Hmtl显示，单词标红
	 * 
	 * @param subtWord
	 * @return
	 */
	public String getSentencesHtmlWithRedKeyWord(String spell) {
		List<SubtSentence> sentences = getSubtSentences(spell);
		StringBuffer stringBuffer = new StringBuffer();
		for (int i = 0; i < sentences.size(); i++) {
			stringBuffer.append(sentences.get(i).getHtmlWithRedKeyWord(spell));
		}
		return stringBuffer.toString();
	}

	/**
	 * 得到句子中的生词
	 * 
	 * @param subtSentence
	 * @return
	 */
	public List<String> getNewWordSpells(SubtSentence subtSentence) {
		List<String> subtWords = new ArrayList<String>();
		for (String spell : this.getLexicon().getSpells()) {
			if (subtSentence.containsWord(spell)) {
				if (null != spell) {
					subtWords.add(spell);
				}
			}
		}
		return subtWords;
	}

	/**
	 * 得到字幕信息
	 */
	public String getInfoView() {
		StringBuffer words = new StringBuffer();
		int total = 0;
		for (String word : this.getLexicon().getSpells()) {
			words.append(word).append(",");
			total++;
		}
		return "总数" + total + "\r\n" + words.toString();
	}

	/**
	 * 如果返回的句子结束时间为空，则为最后一个句子，返回前设置结束时间为大数，永不超出
	 * 
	 * @param currentPlayTime
	 * @return
	 */
	public SubtSentence getCurrentSentence(int currentPlayTime) {
		for (int i = 0; i < this.getSubtSentences().size(); i++) {
			SubtSentence sentence = this.getSubtSentences().get(i);
			// 如果句子的结束时间大于等于当前播放时间，则为应播放字幕句子
			if (sentence.isInTime(currentPlayTime)) {
				return sentence;
			}
		}
		return null;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return this.getName();
	}

	private SubtRepository subtRepository = BeanFactory.getSingleInstance(SubtRepositoryDb.class);
	private LexiconRepository lexiconRepository = BeanFactory.getSingleInstance(LexiconRepositoryDb.class);
}
